/*******************************************************************************
 * Main View                                http://www.codeplex.com/livehome
 *                                          Microsoft Public License (Ms-PL)
 * Authors
 *  - Michael Flanakin  2008  http://www.michaelflanakin.com
 *
 * page
 *  - init : initializes the page
 *  - load : loads the page
 ******************************************************************************/
var page =
{
	//***| private |************************************************************

	container: null,
	bgimg: null,
	timestamp: null,
	updateIcon: null,
	openIcon: null,


	//***| public |*************************************************************

	showFullImage: function() {
		gadget.flyout.show(live.pages.flyout);
	},

	toDateString: function(date) {
		if (date == null) { return null; }
		var months = new Array();
		months[0] = "Jan"; months[1] = "Feb"; months[2] = "Mar"; months[3] = "Apr";
		months[4] = "May"; months[5] = "Jun"; months[6] = "Jul"; months[7] = "Aug";
		months[8] = "Sep"; months[9] = "Oct"; months[10] = "Nov"; months[11] = "Dec";
		return months[date.getMonth()] + " " + date.getDate() + ", " + date.getFullYear();
	},

	padStringLeft: function(value, chr, len) {
		var s = new String(value);
		if (s == null) { s = ""; }
		for (var i = 0; i < len - s.length; i++) { s = chr + s; }
		return s;
	},

	toTimeString: function(date) {
		if (date == null) { return null; }
		var isMorning = (date.getHours() < 11);
		return (isMorning ? (date.getHours() == 0 ? 12 : date.getHours()) : date.getHours() - 12)
			+ ":" + page.padStringLeft(date.getMinutes(), 0, 2)
			+ ":" + page.padStringLeft(date.getSeconds(), 0, 2)
			+ (isMorning ? " am" : " pm");
	},

	load: function() {
		// only update on refresh intervals
		var timeSinceLastUpdate = (page.timestamp[0].date == null) ? live.refreshInterval + 1 : (new Date().getTime() - page.timestamp[0].date.getTime());
		if (timeSinceLastUpdate >= live.refreshInterval) {
			// show image
			var img = live.getImage();

			// if not empty or new, update image/time
			if (!util.string.isEmpty(img) || page.bgimg[0].src == img) {
				page.bgimg[0].src = img;
				page.container.css({ display: "" });
				page.timestamp[0].date = new Date();
			}
		}

		// reset timestamp display to remove visibility change flag
		page.timestamp.text(page.toDateString(page.timestamp[0].date));
		page.timestamp.attr({ title: "Updated on " + page.toDateString(page.timestamp[0].date) + " at " + page.toTimeString(page.timestamp[0].date) });

		// reload every minute to lessen wakeup time after sleep
		setTimeout("page.load();", live.refreshInterval);
	},

	checkForUpdate: function() {
		if (live.isUpdateAvailable()) { page.updateIcon.css({ display: "inline" }); return; }
		setTimeout("page.checkForUpdate();", live.updateCheckInterval);
	},

	init: function() {
		// load controls
		page.body = $("#main");
		page.container = $("#container");
		page.bgimg = $("#bgimg");
		page.timestamp = $("#timestamp");
		page.updateIcon = $("#updateIcon");
		page.openIcon = $("#openIcon");

		// add control event handlers
		page.body.addClass("js");
		page.bgimg.dblclick(page.showFullImage);
		$(".dim").mouseenter(function() { util.opacity.darkenMore(this); }).mouseleave(function() { util.opacity.lightenLess(this); });
		page.openIcon.click(page.showFullImage);

		// validate that all controls have loaded successfully
		if (page.container.length == 0 || page.bgimg.length == 0 || page.timestamp.length == 0) { alert("Not all page controls loaded!"); return; }

		// reload when visibility changes
		if (gadget.host.browser) { $(window).blur(page.visibilityChanged).focus(page.visibilityChanged); }
		else if (gadget.host.sidebar) { System.Gadget.visibilityChanged = page.visibilityChanged; }

		// handle dock/undock events
		gadget.docked = gadget.undocked = function() { page.resize(!gadget.host.docked); };
		if (gadget.host.sidebar) {
			System.Gadget.onDock = gadget.dockedInternal;
			System.Gadget.onUndock = gadget.undockedInternal;
		}
		gadget.docked();

		// load
		page.load();

		// check for update
		page.checkForUpdate(); //setTimeout("page.checkForUpdate();", 5*60*1000); // delay for 5 seconds -- for some reason, validation check doesn't come back the first time
	},
	
	resize: function(maximize){
		var size = (maximize) ? live.undockedSize : live.dockedSize;
		page.body.css({ height: size.height, width: size.width });
		if (maximize) { page.body.addClass("undocked"); } else { page.body.removeClass("undocked"); }
	},

	visibilityChanged: function() {
		page.load();
		page.timestamp.text(page.timestamp.text() + (gadget.host.browser ? " *" : " *"));
		if (gadget.host.sidebar) {
			if (util.string.endsWith(page.timestamp[0].title, " (" + !System.Gadget.visible + ")")) {
				page.timestamp[0].title = page.timestamp[0].title.replace(" (" + !System.Gadget.visible + ")", " (" + System.Gadget.visible + ")");
			}
			else {
				page.timestamp[0].title += " (" + System.Gadget.visible + ")";
			}
		}
	}
};
